const Koa = require('koa');
const router = require('koa-router')(); //注意：引入的方式
const app = new Koa();
const bodyParse = require('koa-bodyparser')
const { responseCommon } = require('./utils')
const { loginInfo, userInfo, userList } = require('./mockData')



router.get('/', function (ctx, next) {
  ctx.body = "Hello koa";
})
router.post('/user/login',(ctx,next)=>{
  let {username, password} = ctx.request.body;
  if (username === loginInfo.username && password == loginInfo.password) {
    ctx.body = responseCommon(true, {
      id: '123456789'
    }, '登录成功')
  } else {
    ctx.body = responseCommon(false, null, '用户名或密码错误')
  }
});

router.get('/user/info', function (ctx, next) {
  ctx.body = responseCommon(true, userInfo, '获取信息成功')
})

router.get('/system/userList', function (ctx, next) {
  ctx.body = responseCommon(true, userList, '获取系统用户列表成功')
})

app.use(bodyParse());
app.use(router.routes()); //作用：启动路由
app.use(router.allowedMethods());
/* 作用： 这是官方文档的推荐用法,我们可以看到router.allowedMethods()用在了路由匹配
router.routes()之后,目的在于：根据ctx.status 设置response 响应头
*/


app.listen(3000,()=>{
  console.log('server is running......');
});